﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using Iso.Common;

namespace Iso.Core
{
    public class Menu
    {
        public List<string> texts;
        public List<Texture> texturesON;
        public List<Texture> texturesOFF;
        public List<Recti> rects;
        public int selectedIndex;
        public event EventHandler OnSelect;

        System.Drawing.Font font;
        System.Drawing.Brush color1, color2;
        Vector2Di position;
        int xSize, ySize, spacing;
        public bool active;
        bool pressed;
        int lastIndex;

        public Menu(System.Drawing.Font font, System.Drawing.Brush color1, System.Drawing.Brush color2,
            Vector2Di position, int xSize, int ySize, int spacing, string[] options)
        {
            texts = new List<string>(options);
            texturesON = new List<Texture>();
            texturesOFF = new List<Texture>();
            rects = new List<Recti>();

            selectedIndex = 0;
            lastIndex = 0;
            pressed = false;
            this.font = font;
            this.color1 = color1;
            this.color2 = color2;
            this.position = position;
            this.xSize = xSize;
            this.ySize = ySize;
            this.spacing = spacing;
        }

        public void Draw()
        {
            for (int i = 0; i < texturesON.Count; i++)
                if (i == selectedIndex)
                    Engine.device.VideoDriver.Draw2DImage(texturesON[i], rects[i], new Recti(0, 0, xSize, ySize), null, null, true);
                else
                    Engine.device.VideoDriver.Draw2DImage(texturesOFF[i], rects[i], new Recti(0, 0, xSize, ySize), null, null, true);
        }

        public void Load()
        {
            for (int i = 0; i < texts.Count; i++)
            {
                rects.Add(new Recti(
                    position.X,
                    position.Y + spacing * i + ySize * i,
                    position.X + xSize,
                    position.Y + spacing * i + ySize * (i + 1)));
                texturesON.Add(Utility.RenderText(texts[i], rects[i].Width, rects[i].Height, font, color1));
                texturesOFF.Add(Utility.RenderText(texts[i], rects[i].Width, rects[i].Height, font, color2));
            }

            active = true;
            Engine.device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);
        }

        bool device_OnEvent(Event evnt)
        {
            if (active && evnt.Type == EventType.Key && evnt.Key.PressedDown == true && !pressed)
            {
                if (evnt.Key.Key == KeyCode.Up)
                    selectedIndex = Utility.Clamp(selectedIndex - 1, 0, texturesON.Count - 1);
                else if (evnt.Key.Key == KeyCode.Down)
                    selectedIndex = Utility.Clamp(selectedIndex + 1, 0, texturesON.Count - 1);
                else if (evnt.Key.Key == KeyCode.Return)
                    if (OnSelect != null)
                    {
                        Engine.soundEngine.Play2D(Engine.baseData + "sounds\\click2.wav");
                        OnSelect(this, new EventArgs());
                    }

                if (lastIndex != selectedIndex)
                {
                    Engine.soundEngine.Play2D(Engine.baseData + "sounds\\click.wav");
                    lastIndex = selectedIndex;
                }

                pressed = true;
            }
            else if (active && evnt.Type == EventType.Key && evnt.Key.PressedDown == false)
                pressed = false;
            else if (active && evnt.Type == EventType.Mouse)
            {
                for (int i = 0; i < rects.Count; i++)
                {
                    if (rects[i].IsPointInside(new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y)))
                    {
                        if (evnt.Mouse.Type == MouseEventType.LeftDown)
                        {
                            Engine.soundEngine.Play2D(Engine.baseData + "sounds\\click2.wav");
                            OnSelect(this, new EventArgs());
                        }
                        else
                        {
                            selectedIndex = i;
                            if (lastIndex != selectedIndex)
                            {
                                Engine.soundEngine.Play2D(Engine.baseData + "sounds\\click.wav");
                                lastIndex = selectedIndex;
                            }
                        }
                    }
                }
            }

            return false;
        }

        public void Delete()
        {
            for (int i = 0; i < texturesON.Count; i++)
                Engine.device.VideoDriver.RemoveTexture(texturesON[i]);
            texturesON.Clear();
            for (int i = 0; i < texturesOFF.Count; i++)
                Engine.device.VideoDriver.RemoveTexture(texturesOFF[i]);
            texturesOFF.Clear();

            active = false;
            Engine.device.OnEvent -= new IrrlichtDevice.EventHandler(device_OnEvent);
        }
    }
}